<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *楼宇管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'楼宇列表'),
	array('op'=>'add','title'=>'添加楼宇'),	
);
if($_RQ['op']=='list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(isset($_RQ['dongshu'])){
		$params['dongshu']=intval($_RQ['dongshu']);
		$return['params']['dongshu']=intval($_RQ['dongshu']);
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','uniacid','area','qishu','dongshu','remark');
	$list=pdo_getslice('jy_smallquan_building',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){

		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>true,'list'=>array(
			array('name'=>'area','title'=>'区域名称','type'=>'string'),
			array('name'=>'qishu','title'=>'期数','type'=>'string'),
			array('name'=>'dongshu','title'=>'栋数','type'=>'string'),
			array('name'=>'remark','title'=>'备注','type'=>'string'),
		),'action' => 
		array(
			 array('type'=>'href','op'=>'add_house','title'=>'批量生成房屋'),
			),),
		'search'=>array(
			array('name'=>'area','width'=>15,'title'=>'区域名称','type'=>'string'),
			array('name'=>'dongshu','width'=>15,'title'=>'栋数','type'=>'string'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}

if($_RQ['op']=='add'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_smallquan_building',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		
	}else{
		$item=array();
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post_add',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回楼宇列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基本信息',
				'list'=>array(
					array('name'=>'area','title'=>'区域名称','type'=>'string'),
					array('name'=>'qishu','title'=>'期数','type'=>'string'),
					array('name'=>'start','title'=>'开始栋数','type'=>'string','help'=>''),
					array('name'=>'end','title'=>'结束栋数','type'=>'string','help'=>''),
					array('name'=>'remark','title'=>'楼宇备注','type'=>'text'),
				)
			),
		)
	);
	$return['htmls']=$htmls;
	//print_r($return['lev_name']);exit;
	exi($return);
}
if($_RQ['op']=='post_add'){
	$postdata=get_postdata();
	if(empty($postdata['area'])){
		exi('请填写区域名称','error');
	}
	if(empty($postdata['qishu'])){
		exi('请填写期数','error');
	}
	if(!$postdata['start']){
		exi('请填写开始栋数','error');
	}
	if(!$postdata['end']){
		exi('请填写结束栋数','error');
	}
	$data=array(
		'end'=>intval($postdata['end']),
		'start'=>intval($postdata['start']),
	);
	$id=intval($postdata['id']);
	if($id){
		//pdo_update('jy_smallquan_building',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		for($i=$data['start'];$i<=$data['end'];$i++){
			$param=array(
				'uniacid'=>$_SESSION['uniacid'],
				'area'=>trim($postdata['area']),
				'qishu'=>trim($postdata['qishu']),
				'remark'=>trim($postdata['remark']),
				'dongshu'=>$i,
				);
			pdo_insert('jy_smallquan_building',$param);
		}
	}
	exi('操作成功！','','list');
}if($_RQ['op']=='add_house'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_smallquan_building',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		
	}else{
		$item=array();
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post_addhouse',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回楼宇列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基本信息',
				'list'=>array(
					array('name'=>'dongshu','title'=>'栋数','type'=>'string','read'=>true),
					array('name'=>'isdanyuan','title'=>'该楼是否拥有单元','type'=>'switch'),
					array('name'=>'start','title'=>'开始单元','type'=>'string','show'=>array('isdanyuan'=>1)),
					array('name'=>'end','title'=>'结束单元','type'=>'string','help'=>'','show'=>array('isdanyuan'=>1)),
					array('name'=>'floor_start','title'=>'开始楼层','type'=>'string','help'=>''),
					array('name'=>'floor_end','title'=>'结束楼层','type'=>'string','help'=>''),
					array('name'=>'room_type','title'=>'户型','type'=>'string','help'=>''),
					array('name'=>'room_num','title'=>'房号','type'=>'string','help'=>''),
					array('name'=>'room_space','title'=>'面积','type'=>'string','help'=>''),
				)
			),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post_addhouse'){
	$postdata=get_postdata();
	if(empty($postdata['area'])){
		exi('请填写区域名称','error');
	}
	if(empty($postdata['qishu'])){
		exi('请填写期数','error');
	}
	if(!$postdata['floor_start']){
		exi('请填写开始楼层','error');
	}
	if(!$postdata['floor_end']){
		exi('请填写结束楼层','error');
	}
	if(empty($postdata['room_type'])){
		exi('请填写户型','error');
	}
	if(empty($postdata['room_num'])){
		exi('请填写房号','error');
	}
	$data=array(
		'end'=>intval($postdata['end']),
		'start'=>intval($postdata['start']),
		'floor_start'=>intval($postdata['floor_start']),
		'floor_end'=>intval($postdata['floor_end']),
	);
	$id=intval($postdata['id']);
	if($id){
		for($i=$data['start'];$i<=$data['end'];$i++){
			for($f=$data['floor_start'];$f<=$data['floor_end'];$f++){
				$param=array(
					'uniacid'=>$_SESSION['uniacid'],
					'bid'=>$id,
					'dongshu'=>intval($postdata['dongshu']),
					'danyuan'=>$i,
					'floor'=>$f,
					'room_type'=>trim($postdata['room_type']),
					'room_num'=>$f.'-'.$postdata['room_num'],
					'room_space'=>$postdata['room_space'],
					);
				pdo_insert('jy_smallquan_house',$param);
			}
		}
	}else{
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_smallquan_building',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		
	}else{
		$item=array();
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回楼宇列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基本信息',
				'list'=>array(
					array('name'=>'area','title'=>'区域名称','type'=>'string'),
					array('name'=>'qishu','title'=>'期数','type'=>'string'),
					array('name'=>'dongshu','title'=>'栋数','type'=>'string','help'=>''),
					array('name'=>'remark','title'=>'楼宇备注','type'=>'text'),
				)

			),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(empty($postdata['area'])){
		exi('请填写区域名称','error');
	}
	if(empty($postdata['qishu'])){
		exi('请填写期数','error');
	}
	if(!$postdata['dongshu']){
		exi('请填写栋数','error');
	}
	$data=array(
		'area'=>trim($postdata['area']),
		'qishu'=>trim($postdata['qishu']),
		'dongshu'=>intval($postdata['dongshu']),
		'remark'=>trim($postdata['remark']),
	);
	
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_smallquan_building',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_smallquan_building',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}

